<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeRoleDialog" @open="openRoleDialog">

    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <!-- <el-checkbox label="110">管理员</el-checkbox>
      <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->

      <!-- label决定当前选中的值 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="closeRoleDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees.js'
import { getUserRoleIds } from '@/api/user'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 可选角色列表数据
      roleList: [],
      // 已选角色列表
      checkList: []
    }
  },
  methods: {
    closeRoleDialog() {
      this.$emit('close-role')
    },

    // 利用open事件自执行
    async openRoleDialog() {
      const res = await getRoleList({
        page: 1,
        pagesize: 100
      })
      this.roleList = res.rows
      // 获取已选角色列表回填
      const detail = await getUserRoleIds(this.curId)
      this.checkList = detail.roleIds
    },

    // 提交勾选角色
    async submit() {
      await assignRoles({
        id: this.curId,
        roleIds: this.roleIds
      })
      this.$message.success('分配角色成功')
      // 关闭弹窗
      this.closeRoleDialog()
      // 清空已选
      // this.roleIds = []
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
